package com.origin.ebuy.forg.web.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.origin.ebuy.bargd.service.good.IgoodService;
import com.origin.ebuy.bargd.service.good.impl.GoodServiceImpl;
import com.origin.ebuy.forg.web.BaseServlet;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 做商品的管理操作
 * Servlet implementation class UpdateGood
 */
public class UpdateGoodServlet extends BaseServlet implements Servlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public UpdateGoodServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
		IgoodService service = new GoodServiceImpl();
		String type = request.getParameter("type");
		System.out.println("==========");
		//添加操作
		if("1".equals(type)){
			String name = request.getParameter("name");
			String price = request.getParameter("price");
			String number = request.getParameter("number");
			String image = request.getParameter("imgeurl");
			
			JSONObject object = new JSONObject();
			object.put("name", name);
			object.put("price", price);
			object.put("number", number);
			object.put("image", image);
			
			try {
				service.addGood(object);
				response.getWriter().print("1");
			} catch (SQLException e) {
				e.printStackTrace();
				response.getWriter().print("-1");
			}
			
		//修改商品	
		}else if("2".equals(type)){
			String name = request.getParameter("name");
			String price = request.getParameter("price");
			String number = request.getParameter("number");
			String image = request.getParameter("imgeurl");
			String id = request.getParameter("id");
			String state = request.getParameter("state");
			try {
				service.updateGood(id, name, price, number, image,state);
				response.getWriter().print("1");
			} catch (SQLException e) {
				e.printStackTrace();
				response.getWriter().print("-1");
			}
			
		//删除商品	
		}else if("3".equals(type)){
			String id = request.getParameter("id");
			try {
				service.deleteGood(id);
				response.getWriter().print("1");
			} catch (SQLException e) {
				e.printStackTrace();
				response.getWriter().print("-1");
			}
		}
		
	}

}
